package com.idanchuang.component.logback.converter;


import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.idanchuang.component.logback.appender.DcILoggingEvent;

import static com.idanchuang.component.logback.web.filter.RequestStartTimeFilter.REQUEST_START_THREAD_LOCAL;

public class DurationPatternConverter extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent le) {
        if (le instanceof DcILoggingEvent) {
            return ((DcILoggingEvent) le).getDuration();
        }
        return figure(le);
    }

    public static String figure(ILoggingEvent iLoggingEvent) {
        Long startTime = REQUEST_START_THREAD_LOCAL.get();
        if (startTime != null) {
            return String.valueOf(System.currentTimeMillis() - startTime);
        }
        return "-";
    }

}
